procedure sort(l, r : longint);

var
	i, j, x, y : longint;

begin
	i := l;
	j := r;
	x := a[l + random(r - l + 1)];
	repeat
		while a[i] < x do inc(i);
		while a[j] > x do dec(j);
		if i <= j then begin
			y := a[i];
			a[i] := a[j];
			a[j] := y;
			inc(i);
			dec(j);
		end;
	until i > j;
	if l < j then sort(l, j);
	if i < r then sort(i, r);
end;